package com.geovis.emergency.spd.biz.basic.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.basic.mapper.BasicShareDocMapper;
import com.geovis.emergency.spd.biz.basic.mapper.BasicShareFolderMapper;
import com.geovis.emergency.spd.biz.basic.service.IBasicShareFolderService;
import com.geovis.emergency.spd.entity.basic.entity.BasicShareDoc;
import com.geovis.emergency.spd.entity.basic.entity.BasicShareFolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 基础模块-共享文档目录 服务实现类
 * </p>
 *
 * @author Lonsho
 * @since 2022-07-12
*/

@Service
@RequiredArgsConstructor
@Slf4j
public class BasicShareFolderServiceImpl extends ServiceImpl<BasicShareFolderMapper, BasicShareFolder> implements IBasicShareFolderService {

    private final BasicShareDocMapper basicShareDocMapper;

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void removeByIdList(Set<String> idList) {
        if (CollUtil.isEmpty(idList)) {
            return;
        }
        //1、刪除本身
        removeByIds(idList);

        //3、刪除孩子节点
        List<BasicShareFolder> list = baseMapper.selectList(Wrappers.lambdaQuery(BasicShareFolder.class).select(BasicShareFolder::getId).in(BasicShareFolder::getFolderId, idList));
        Set<String> childrenIdList = CollUtil.set(false);
        list.forEach(basicDutyPost -> childrenIdList.add(basicDutyPost.getId()));
        removeByIdList(childrenIdList);

        // 删除文件
        basicShareDocMapper.delete(Wrappers.lambdaQuery(BasicShareDoc.class).in(BasicShareDoc::getFolderId,idList));
    }
}
